﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace PreContractWeb.Task
{
    public partial class ImportOrionForecast : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.tabDocument.Attributes.Add("onSelectedIndexChange", "ResizeDivForScroll('divGrid');ResizeDivForScroll('divGridTwo');ResizeDivForScroll('divGridThree');");
        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (this.uplFile.FileName == null || this.uplFile.FileName.Trim() == "")
            {
                ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, this.GetType(), "", "<script>alert('" + "请选择需要上传的文件.\\n\\nPlease select the file to upload." + "')</script>",false);
                return;
            }

            if (System.IO.Path.GetExtension(this.uplFile.FileName).ToLower() != ".xls")
            {
                ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, this.GetType(), "", "<script>alert('" + "上传文件的扩展名必须是xls.\\n\\nThe upload file extension must be xls." + "')</script>", false);
                return;
            }

            string filePath = BEGDC.MIS.Office.Configuration.UploadPath + uplFile.FileName;

            this.uplFile.SaveAs(filePath);

            try
            {

                BEGDC.MIS.Office.IOffice ioffice = (BEGDC.MIS.Office.IOffice)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_OrionData_Service];
                DataSet dsUpload = ioffice.GetDataFromFile(filePath);

                System.Data.DataTable dtUpload = null;
                System.Data.DataTable dtUpdated = null;
                System.Data.DataTable dtMissing = null;
                System.Data.DataTable dtNotExisted = null;

                if (dsUpload != null && dsUpload.Tables.Count > 0)
                    dtUpload = dsUpload.Tables[0];

                BEGDC.PreContract.BLL.IPreContractInfo contractservice = (BEGDC.PreContract.BLL.IPreContractInfo)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_PreContractInfo_Service];
                string strMessage = string.Empty;
                contractservice.UploadOrionInfo(dtUpload, out dtMissing, out dtUpdated,out dtNotExisted, out strMessage);


                gvMain.DataSource = dtMissing;
                gvMain.DataBind();
                this.totalSpan.InnerText = dtMissing.Rows.Count.ToString();

                gvUpdated.DataSource = dtUpdated;
                gvUpdated.DataBind();
                this.totalSpan_Updated.InnerText = dtUpdated.Rows.Count.ToString();

                gvNotExisted.DataSource = dtNotExisted;
                gvNotExisted.DataBind();
                this.totalSpan_NotExists.InnerText = dtNotExisted.Rows.Count.ToString();

                BaseFunction.CreateLog(BEGDC.PreContract.BLL.OperationLog.OperationType.ImportOrion, BEGDC.PreContract.BLL.OperationLog.OperationType.ImportOrion.ToString(), User.Identity.Name);

            }
            catch (Exception ex)
            {

                ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, this.GetType(), "",  "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>",false);
            }

        }

        protected void gvMain_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }


        protected void gvUpdated_RowDataBound(object sender, GridViewRowEventArgs e)
        {


        }
    }
}
